package com.shixu.framework.plugins.weixin.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shixu.framework.core.AppContext;
import com.shixu.framework.plugins.weixin.WeChat;
import com.shixu.framework.plugins.weixin.service.MenuService;
import com.shixu.framework.plugins.weixin.service.MessageService;
import com.shixu.framework.plugins.weixin.service.OauthService;
import com.shixu.framework.plugins.weixin.util.Tools;
import com.shixu.framework.plugins.weixin.util.WeixinConstant;

@Controller
@RequestMapping
public class WeixinApi {
	
	private static Logger log = LoggerFactory.getLogger(WeixinApi.class);
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param TOKEN 自定义token
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/api/weixin/signature" ,method ={GET,POST})
	public void doGet(HttpServletRequest request, HttpServletResponse response,String TOKEN) throws ServletException, IOException {
		// 微信加密签名
		String signature = request.getParameter("signature");
		// 随机字符串
		String echostr = request.getParameter("echostr");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");

		log.info("signature：" + signature);
		log.info("echostr：" + echostr);
		log.info("timestamp：" + timestamp);
		log.info("nonce：" + nonce);
		log.info("TOKEN：" + TOKEN);
		
		Tools.checkSignature(TOKEN, signature, timestamp, nonce);
	}
	
	@RequestMapping(value = "/test/weixin/sendTest" ,method ={GET,POST})
	public void sendMsg(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String msg = request.getParameter("msg");
		String to= request.getParameter("to");
//		msg= "测试一下字符编码问题";
		JSONObject json = new JSONObject();
		
		String appid = AppContext.getInstance().getString("appid");
        String secret = AppContext.getInstance().getString("appsecret");
		
		String token = WeChat.getAccessToken(appid,secret).getToken();
		
		log.info("token:"+token);
		String url = WeixinConstant.SEND_MSG_URL.replaceAll("ACCESS_TOKEN", token);
		log.info(url);
		log.info(json.toString());
		
		MenuService menu = new MenuService();
		
		try {
			menu.deleteMenu(token);
			OauthService oauth = new OauthService();
			
			if (!menu.createMenu(token, "{\"button\":[{\"type\":\"view\",\"name\":\"view\",\"url\":\""+ oauth.getCode() +
					"\"},{\"type\":\"click\",\"name\":\"click\",\"key\":\"orderCreate\"}]}")) {
				System.out.println("create menu error");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		MessageService message = new MessageService();
		String resp = message.sendText(token, to, msg);
//		JSONObject resp= sendText(url, "POST", json.toString());
		log.info(resp);
		
	}

}
